#!/usr/bin/perl

$total_packet = 480;

@time[7600];
@node[7600];
@packet[7600];
@bitmap[480];

$startTime = 0;
	$counter = 0;
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";
open fout, ">$ARGV[0]\\total" or die "can not open file $ARGV[0]\\total!\n";	
while ($file = readdir $dir) {

	$time = 0;
	$i = 0;

	$has = 0;
	$node= 0;
	$file_path = "$ARGV[0]\\$file";
	#print "$file";
	@tmp = split /\./,$file;
	$lstpage = -1;
	$page = 0;

	for($k=0;$k<$total_packet;$k++)
	{
		@bitmap[$k] = 0;
	}

	
	#if (-f $file_path) {
	if (@tmp[1] eq "log" && @tmp[0] eq "0") {	
	  open fin, "<$file_path" or die "can not open file $file_path!\n";
	  open fout1, ">$ARGV[0]\\@tmp[0].info" or die "can not open file $ARGV[0]\\@tmp[0].info!\n";	
		while (<fin>) {
			chomp;
			@rs = split;

			$type = hex("$rs[9]");

			if($type==0xCE && @tmp[0]==0 && $startTime==0){
				$startTime = hex("$rs[13]$rs[14]$rs[15]$rs[16]");
				print "start: $startTime\n";
			}
			if( $type==0x07 && hex("$rs[10]")==0xCC){
				$page = hex("$rs[17]$rs[18]$rs[19]$rs[20]");
				if($page!=$lstpage) {
					$lstpage =$page;
					$time = hex("$rs[13]$rs[14]$rs[15]$rs[16]") - $startTime;
					$time = $time/1000;
					$node = hex("$rs[11]$rs[12]");
					#$has = 1;
					#$counter++;
					print fout "$time $page $node\n";
					#print "$time $page $node\n";

					for($k=0;$k<$total_packet;$k++)
					{
						@bitmap[$k] = 0;
					}
				}
			}
			if(($type==0xCE || $type==0x32) && $counter <480) {
					$time = hex("$rs[13]$rs[14]$rs[15]$rs[16]") - $startTime;
					$time = $time/1000;
					$node = hex("$rs[11]$rs[12]");
					$packetNo = hex("$rs[17]$rs[18]$rs[19]$rs[20]");

					$xxx = $page +1;
					print "$time $packetNo $xxx\n";

					if(@bitmap[$packetNo]==0){
						@bitmap[$packetNo]=1;
						print fout1 "$time $packetNo $nodeID\n";
						$counter++;
					}

			}

			

		} #while<fin>
		print "$counter\n";
		#print "$counter\n";
	  close fin;

	}
}
	  close fout;
close $dir;
print "$counter\n";

